package com.sysu.minsysu.xyxw;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.sysu.R;

public class YiDongXueXi extends Activity{
	
	final int mButtonCount=4;
	GridView mGridView;
	SimpleAdapter mAdapter;
	@Override
	public void onCreate( Bundle savedInstanceState ){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.yidongxuexi);
		mGridView = (GridView)findViewById(R.id.gridview);
		
		mAdapter = new SimpleAdapter(this, getData(), R.layout.view_appmain, new String[]{ "image", "text" }, new int[]{ R.id.imageView, R.id.textView });
		mGridView.setAdapter(mAdapter);
		mGridView.setNumColumns(2);
		mGridView.setOnItemClickListener( new MyOnItemClickListener() );
		Toast.makeText(this, "因服务器尚不支持外网访问，请在打开视频之前确认您已连接到中大校园网络！", Toast.LENGTH_LONG).show();
	}
	
	private class MyOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
			Resources res = getResources();
			String[] classNames=null;
			int[] length=null;
			String server= "rtsp://222.200.180.213/";
			String[][] paths=null;
			if( position== 0 ){
				classNames = res.getStringArray(R.array.openclass_temp_allnames);
				length = new int[]{19,21,8,10,21};
				paths = new String[classNames.length][];
				for( int i=0; i<length.length; i++ ){
					paths[i] = new String[ length[i] ];
					for( int j=0; j<length[i]; j++ ){
						//paths[i][j] = "第" + j + "课";
						paths[i][j] = server + "justice" + (j%10+1) + ".mp4";
					}
				}
			}else {
				Toast.makeText(YiDongXueXi.this, "这个栏目的视频还在完善中，目前仅能访问公开课栏目，以后将逐步完善新栏目，敬请期待", Toast.LENGTH_LONG).show();
				return ;
			}
			 
			Intent intent = new Intent( YiDongXueXi.this, VideoList.class);
			Bundle bundle = new Bundle();
			bundle.putIntArray("length", length);
			bundle.putStringArray("names", classNames);
			//bundle.putSerializable("paths", paths);
			intent.putExtras(bundle);
			startActivity(intent);			
			
		}
	}

	private List< Map<String, Object> > getData(){
		List< Map<String, Object>> list = new ArrayList<Map<String,Object>>();
			
		for( int i=0; i<mImgIds.length; i++ ){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("image", mImgIds[i]);
		map.put("text", mNames[i]);
		list.add(map);
		}
			
		return list;
	}
	
	int[] mImgIds = new int[]{
			R.drawable.zixungongxiang_yidongxuexi_gongkaike,
			R.drawable.zixungongxiang_yidongxuexi_jiangzuo,
			R.drawable.zixungongxiang_yidongxuexi_jingpinkecheng,
			R.drawable.zixungongxiang_yidongxuexi_pinweizhongda,
	};
	
	String[] mNames = new String[]{
			"公开课",
			"讲座",
			"精品课程",
			"品味中大",
	};
}
	
